package com.duowan.mspider.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpCoreContext;
import org.apache.http.util.EntityUtils;

import java.io.IOException;


public class MyHttpClient {

	public static final RequestConfig Config = RequestConfig.custom()
													   .setConnectionRequestTimeout(3000)
													   .setConnectTimeout(3000)
													   .setSocketTimeout(3000).build();

	public static String getSougouRealUrlInfo(String url) {
		String htmlContent = doGet(url);
		if (StringUtils.isBlank(htmlContent) || htmlContent.indexOf("';var body") < 0) return "";

		int endIndex = htmlContent.indexOf("';var body");
		if (htmlContent.indexOf("https:") > 0) {
			String realUrl = htmlContent.substring(htmlContent.indexOf("https:"), endIndex);
			return realUrl;
		}
		if (htmlContent.indexOf("http:") > 0) {
			String realUrl = htmlContent.substring(htmlContent.indexOf("http:"), endIndex);
			return realUrl;
		}
		return "";
	}

	public static String getRedirectUrl(String url){
		String currentUrl = "";
		HttpGet httpget = new HttpGet(url);
		HttpContext context = new BasicHttpContext();
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(Config).build();
		try {
			HttpResponse response = httpClient.execute(httpget, context);
			if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK)
				throw new IOException(response.getStatusLine().toString());
			HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(
					HttpCoreContext.HTTP_REQUEST);
			HttpHost currentHost = (HttpHost)  context.getAttribute(
					HttpCoreContext.HTTP_TARGET_HOST);
			currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq.getURI());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return currentUrl;
	}

	/**
	 * 获取重定向之后的网址信息
	 */
	public static String doGet(String url){
		String htmlContent = "";
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(Config).build();
		HttpContext httpContext = new BasicHttpContext();
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpGet, httpContext);
			HttpEntity entity = response.getEntity();
			if (null != entity) {
				htmlContent = EntityUtils.toString(entity, ContentType.getOrDefault(entity).getCharset());
				EntityUtils.consume(entity);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return htmlContent;
	}

	private static String closeAndReturn(CloseableHttpResponse response) throws IOException {
		try {
			HttpEntity entity = response.getEntity();
			String result = EntityUtils.toString(entity);
			EntityUtils.consume(entity);
			return result;
		} finally {
			response.close();
		}
	}

	public static void main(String[] args) throws Exception {
		//doGet("http://m.sogou.com/bill_cpc?v=1&p=WJ80$xBzh7PedZL0y3x9YDmAneZ30Y7mGLI12jeoBRFYTaXHlrsV8@5aarGfE58TAt50BWW8$wjF0ToKGLgDBAeYeC0VtKbrCKHbqPdEC7FwUBhKXOdzXO0yeQAbUqJvUphOrCMvrINzXIAXOihXgijYXO$7XCeYqqutXJ3ObLklxwYpWP6OmFu$euciuvuLPkzpJoBuOAYFp9dnW27U2wmS8talUjW8Yl40OR@4NkPw7g9eVk6eQ60etFVuj49zuImBpSuzpqmleGm9pwClNawyoEDUneNFSr9yofpUxPNayqOuAIjjozqx7opVABOPo6Qh7bOV97PmlhG0lflBVIBui7YkaW6OyoZpyanBjrnVcafvcmVArxxA28wZGFyLGlFe3P9DahfuH4yPVe5rsRvrIaZD$fv6J3H3$DF9SjjBxcbiwllv7DoRhPHzJy4Vw5JlVOjbNqpvNQ9A6JFAiQwI3UcHVp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